-
Notifications
You must be signed in to change notification settings - Fork 8.3k
kernel: Add Kconfig option to disable LTO for kernel sources #99124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
kernel: Add Kconfig option to disable LTO for kernel sources #99124
Conversation
| # Append the injected code to the end of the file | ||
| file(APPEND ${KERNEL_CMAKE} "${INJECT_CODE}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uh, why is this needed? The whole append/remove thing feels a bit hacky to be honest, hope we can find a different solution, you just need the -fno-lto -g in the build command line for the files above right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason is that setting -fno-lto elsewhere (e.g. board-level CMake) didn’t work likely because of build order issues. Kernel sources were still built with LTO, so I had to apply it directly here to make sure those files compile without LTO and can reside in RAM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok so this is basically touching the source file to somehow convince cmake to build it again with the new flag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct, it temporarily modifies the kernel CMake file to ensure those specific sources are rebuilt with -fno-lto. After the build configuration is complete, the injected changes are automatically cleaned up to restore the original file state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. I don't really think it's a good idea to try and modify non generated files from the build script, @tejlmand do you have a better approach to suggest?
Would we not be able to get away with adding a Kconfig flag and the options in the kernel CMakeLists.txt file directly? If this SoC has a scenario where having a kernel specific option to selectively not do LTO it may be handy for other devices too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of modifying the kernel's CMake file, what about creating a separate CMake file that defines the no-LTO and other desired properties?
You could have the kernel CMake file do a conditional include based on a CONFIG option being set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @fabiobaltieri and @keith-zephyr for the helpful suggestions.
I’ve updated the implementation accordingly. It now adds a Kconfig option to control the behavior, and the logic is handled inside kernel/CMakeLists.txt without modifying it from external scripts. Additionally, I’ve added a separate kernel_no_lto.cmake file to define the no-LTO settings.
d69e872 to
e660d0d
Compare
e660d0d to
123ab0b
Compare
kernel/kernel_no_lto.cmake
Outdated
| NOT src MATCHES "errno\\.c$" AND | ||
| NOT src MATCHES "fatal\\.c$" AND | ||
| NOT src MATCHES "init\\.c$") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these files be added to the LTO_ALLOWLIST as the default settings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I’ve added them to the default LTO_ALLOWLIST. Thanks!
123ab0b to
d33aafc
Compare
|
@fabiobaltieri Please revisit, thanks! |
d33aafc to
7171255
Compare
fabiobaltieri
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tejlmand does this look like an acceptable approach?
|
Just wondering...
Can't you do a partial linking with LTO of the kernel and the
application separately, then join them with a final link?
|
kernel/Kconfig
Outdated
| help | ||
| List of kernel source filenames that should retain LTO even when | ||
| CONFIG_KERNEL_NO_LTO is enabled. | ||
| Example: CONFIG_KERNEL_LTO_ALLOWLIST="init.c errno.c fatal.c" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you want to make these files the default setting? Otherwise you could expand the help text to suggest this list as a good default as these features don't benefit from the performance gain of running in RAM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we set default "init.c errno.c fatal.c" for CONFIG_KERNEL_LTO_ALLOWLIST, users would still need to override the entire string when adding more files. Kconfig does not support appending to the default value, so it cannot be extended incrementally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acknowledged. As a follow up PR, you could enable CONFIG_KERNEL_NO_LTO on the ITE eval board so that there is a reference implementation for users to copy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, once this PR is merged, I’ll create a follow-up PR to enable.
@npitre , Thanks for the suggestion. My concern is that even with partial linking, once LTO is applied, the compiler may still inline or merge functions across the participating kernel files. This means I can no longer obtain the original libkernel object files or their sections that must be placed in RAM. My current approach explicitly applies -fno-lto to the RAM-critical kernel sources, which guarantees their object layout remains intact. This is required so we can reliably place those routines into RAM for performance. |
ed42e77
7171255 to
ed42e77
Compare
|
@tejlmand, Any feedback would be greatly appreciated. |
kernel/Kconfig
Outdated
| for timing-critical routines or context switch paths. | ||
|
|
||
| if KERNEL_NO_LTO | ||
| config KERNEL_LTO_ALLOWLIST |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be done in CMake using functions not Kconfig
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. I’ve removed the KERNEL_LTO_ALLOWLIST Kconfig option and switched to using a CMake function as recommended.
Some SoCs require kernel code to be placed in RAM, which makes link-time optimization (LTO) unsuitable for these files. Disabling LTO allows the affected code to be linked as separate objects and placed in specific memory regions. Running kernel code from RAM can improve execution performance, especially for timing-critical routines or context switch paths. Signed-off-by: Tim Lin <tim2.lin@ite.corp-partner.google.com>
Select KERNEL_NO_LTO only when LTO is enabled. This ensures proper handling when kernel code is placed in RAM. Signed-off-by: Tim Lin <tim2.lin@ite.corp-partner.google.com>
5e9e169
ed42e77 to
5e9e169
Compare
|



Some SoCs require kernel code to be placed in RAM, which makes link-time optimization (LTO) unsuitable for these files.
Disabling LTO allows the affected code to be linked as separate objects and placed in specific memory regions.
Running kernel code from RAM can improve execution performance, especially for timing-critical routines or context switch paths.
Another commit is to enable
KERNEL_NO_LTOwhenSOC_IT8XXX2_KERNEL_IN_RAMandLTOare enabled on the IT8XXX2 chip.